// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.undation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
package org.columba.mail.gui.charset;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Hashtable;

import javax.swing.ButtonGroup;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

import org.columba.api.gui.frame.IFrameMediator;
import org.columba.core.charset.CharsetEvent;
import org.columba.core.charset.CharsetListener;
import org.columba.core.charset.CharsetOwnerInterface;
import org.columba.core.gui.base.CMenu;
import org.columba.core.gui.menu.IMenu;
import org.columba.core.logging.Logging;
import org.columba.core.resourceloader.ImageLoader;
import org.columba.mail.util.MailResourceLoader;

/**
 * Creates a menu containing submenus with different charsets from which the
 * user can choose.
 */

public class CharacterEncodingSubMenu extends IMenu implements ActionListener,
		CharsetListener {

	// String definitions for the charsetnames
	// NOTE: these are also used to look up the
	// menuentries from the resourceloader
	private static final String[] CHARSET_STRINGS = {

	// Global # 1
			"UTF-8", "UTF-16", "US-ASCII",

			// West Europe # 4
			"windows-1252", "ISO-8859-1", "ISO-8859-15", "ISO-8859-7",
			"windows-1253", "ISO-8859-3",

			// East Europe # 10
			"ISO-8859-4", "ISO-8859-13", "windows-1257", "ISO-8859-2",
			"ISO-8859-5", "KOI8-R", "windows-1251",

			// East Asian # 17
			"GB2312", "GBK", "GB18030", "Big5", "Big5-HKSCS", "EUC-TW",
			"EUC-JP", "Shift_JIS", "ISO-2022-JP", "MS932", "EUC-KR", "JOHAB",
			"ISO-2022-KR",

			// West Asian # 30
			"TIS620", "ISO-8859-9", "windows-1254", "windows-1258" };

	private static final Charset[] CHARSETS = createCharsetArray();

	private static final String[] groups = { "global", "westeurope",
			"easteurope", "eastasian", "seswasian" };

	private static final int[] groupOffset = { 0, 3, 9, 16, 29, 33 };

	private ButtonGroup group;

	private Hashtable hashtable;

	/**
	 * The menu item showing the currently selected charset.
	 */
	// protected CharsetMenuItem selectedMenuItem = new CharsetMenuItem(null);
	/**
	 * Creates a new menu for choosing charsets. The passed IFrameMediator
	 * instance needs to implement CharsetOwnerInterface.
	 */
	public CharacterEncodingSubMenu(IFrameMediator mediator) {
		super(mediator, MailResourceLoader.getString("menu", "mainframe",
				"menu_view_charset"), "menu_view_charset");

		//setIcon(ImageLoader.getImageIcon("stock_font_16.png"));

		group = new ButtonGroup();

		hashtable = new Hashtable();

		add(createMenuItem(null));

		// Automatic Generation of Groups
		CMenu subsubMenu;

		for (int i = 0; i < groups.length; i++) {
			subsubMenu = new CMenu(MailResourceLoader.getString("menu",
					"mainframe", "menu_view_charset_" + groups[i]),
					"menu_view_charset_" + groups[i]);
			add(subsubMenu);

			for (int j = groupOffset[i]; j < groupOffset[i + 1]; j++) {
				if (CHARSETS[j] != null) {
					JMenuItem item = createMenuItem(CHARSETS[j]);
					subsubMenu.add(item);
				}
			}
		}

		((CharsetOwnerInterface) controller).addCharsetListener(this);

		// simulate charset changed to initialize selectedMenuItem
		charsetChanged(new CharsetEvent(this,
				((CharsetOwnerInterface) controller).getCharset()));

	}

	private static Charset[] createCharsetArray() {
		Charset[] result = new Charset[CHARSET_STRINGS.length];
		for (int i = 0; i < CHARSET_STRINGS.length; i++) {
			try {
				result[i] = Charset.forName(CHARSET_STRINGS[i]);
			} catch (UnsupportedCharsetException e) {
				if (Logging.DEBUG)
					e.printStackTrace();

				result[i] = null;
			}
		}
		return result;
	}

	/**
	 * Creates a menu item and registers action and mouse listeners.
	 */
	protected CharsetMenuItem createMenuItem(Charset charset) {
		CharsetMenuItem menuItem = new CharsetMenuItem(charset);
		group.add(menuItem);
		menuItem.addActionListener(this);
		if (charset != null)
			hashtable.put(charset, menuItem);

		return menuItem;
	}

	/**
	 * Called when a charset is chosen from the menu.
	 */
	public void actionPerformed(ActionEvent e) {
		((CharsetOwnerInterface) controller).setCharset(((CharsetMenuItem) e
				.getSource()).getCharset());
	}

	/**
	 * Updates the selectedMenuItem according to the chosen charset.
	 */
	public void charsetChanged(CharsetEvent e) {
		// selectedMenuItem.setCharset(e.getCharset());
		if (e.getCharset() == null) {
			JRadioButtonMenuItem item = (JRadioButtonMenuItem) getMenuComponent(0);
			item.setSelected(true);

		} else {
			if (hashtable.containsKey(e.getCharset())) {
				CharsetMenuItem menuItem = (CharsetMenuItem) hashtable.get(e
						.getCharset());
				menuItem.setSelected(true);
			}
		}
	}
}